<?php
class Model_Eav_Resource_Abstract
    extends Eav
{
    public function __construct()
    {
        $model = new Zend_Db_Table($this->_entityTableName);
        parent::__construct($model);
    }

    public function load(&$model, $value, $field = null)
    {
        $resourceModel = new Zend_Db_Table($this->_entityTableName);
        $select = $resourceModel->select();

        if($field) {
            $select->where($this->formatFieldToWhere($field),$value);
        } else {
            $select->where($this->formatFieldToWhere($this->_entityFieldId),$value);
        }
        $select->limit(1);
        $data = $resourceModel->fetchRow($select);

        $optionsModel = new Zend_Db_Table($this->_optionTableName);
        $options = $optionsModel->fetchAll();

        $staticData = $data->toArray();
        foreach ($staticData as $key=>$value) {
            $model->setOriginalData($key,$value);
        }

        foreach ($options as $_option) {
            $model->setOriginalData($_option->code,$this->getOptionValue($data,$_option));
        }

        $model->setPrimary($this->_entityFieldId);
        $model->setIsNew(false);

        return $this;
    }

    public function save(&$model)
    {
        /* @var $model Model_Core_Abstract */
        /* @var $resourceModel Zend_Db_Table */
        $resourceModel = new Zend_Db_Table($this->_entityTableName);
        if($model->getIsDataChange()) {
            $fields = $resourceModel->getAdapter()->describeTable($this->_entityTableName);
            $staticData = $model->getData();

            if(!isset($staticData['created_at'])) {
                $staticData['created_at'] = now();
            }

            $staticData['updated_at'] = now();

            foreach ($staticData as $key=>$value) {
                if(!array_key_exists($key, $fields)) {
                    unset($staticData[$key]);
                }
            }

            $modelId = null;

            if($model->getIsNew()) {
                $resourceModel->insert($staticData);
                $modelId = $resourceModel->getAdapter()->lastInsertId($this->_entityTableName);
            } else {
                $modelId = $model->getId();
                $where = $this->_entityTableName.'.'.$this->_entityFieldId.'='.$modelId;
                $resourceModel->update($staticData, $where);
            }

            $items = $resourceModel->find(array($modelId));

            $data = $model->getData();
            foreach ($items as $_item) {
                foreach ($data as $key=>$value) {
                    $this->setOptionValue($_item, $key, $value);
                }
            }

            $model->setIsDataChange(false);
            $model->setIsNew(false);
        }
    }

    public function deleteModel(&$model)
    {
        /* @var $model Model_Core_Abstract */
        /* @var $resourceModel Zend_Db_Table */
        if(!$model->getIsNew()) {
            $resourceModel = new Zend_Db_Table($this->_entityTableName);
            $resourceModel->delete($this->_entityTableName.'.'.$this->_entityFieldId.'='.$model->getId());
            $model->setIsNew(true);
            $model->setData(array());
            $model->setIsDataChange(false);
        }
    }

    protected function formatFieldToWhere($field)
    {
        return $this->_entityTableName.'.'.$field.' = ?';
    }
}